package erfgame.frontend.desktop.view.svg;

import java.awt.Cursor;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.geom.Dimension2D;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.BridgeExtension;
import org.apache.batik.bridge.ExternalResourceSecurity;
import org.apache.batik.bridge.ScriptSecurity;
import org.apache.batik.bridge.UserAgent;
import org.apache.batik.dom.svg.SVGOMDocument;
import org.apache.batik.gvt.event.EventDispatcher;
import org.apache.batik.gvt.text.Mark;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.util.ParsedURL;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGAElement;
import org.w3c.dom.svg.SVGDocument;

public class SVGImageView extends JSVGCanvas {
	
	private static final Logger log = Logger.getLogger(SVGImageView.class.getName());
	
	public SVGImageView() {
		super( null, true, false );
		this.setRecenterOnResize(true);
		this.setDisableInteractions(true);
		this.setDoubleBuffered(true);
		this.setDoubleBufferedRendering(true);
	}
	
	

	@Override
	protected BridgeContext createBridgeContext(SVGOMDocument arg0) {
		BridgeContext context = super.createBridgeContext(arg0);
		context.setAnimationLimitingCPU(0.5f);
		return context;
	}



	@Override
	protected UserAgent createUserAgent() {
		final UserAgent proxied = super.createUserAgent();
		return new UserAgent(){

			public void checkLoadExternalResource(ParsedURL arg0, ParsedURL arg1)
					throws SecurityException {
				proxied.checkLoadExternalResource(arg0, arg1);
				
			}

			public void checkLoadScript(String arg0, ParsedURL arg1,
					ParsedURL arg2) throws SecurityException {
				proxied.checkLoadScript(arg0, arg1, arg2);
			}

			public void deselectAll() {
				proxied.deselectAll();
			}

			public void displayError(Exception ex) {
				log.log(Level.WARNING, "error rendering SVG", ex);
			}

			public void displayMessage(String msg) {
				log.info("display message: "+msg);
			}

			public String getAlternateStyleSheet() {
				return proxied.getAlternateStyleSheet();
			}

			public float getBolderFontWeight(float arg0) {
				return proxied.getBolderFontWeight(arg0);
			}

			public SVGDocument getBrokenLinkDocument(Element arg0, String arg1,
					String arg2) {
				return proxied.getBrokenLinkDocument(arg0, arg1, arg2);
			}

			public Point getClientAreaLocationOnScreen() {
				return proxied.getClientAreaLocationOnScreen();
			}

			public String getDefaultFontFamily() {
				return proxied.getDefaultFontFamily();
			}

			public EventDispatcher getEventDispatcher() {
				return proxied.getEventDispatcher();
			}

			public ExternalResourceSecurity getExternalResourceSecurity(
					ParsedURL arg0, ParsedURL arg1) {
				return proxied.getExternalResourceSecurity(arg0, arg1);
			}

			public String getLanguages() {
				return proxied.getLanguages();
			}

			public float getLighterFontWeight(float arg0) {
				return proxied.getLighterFontWeight(arg0);
			}

			public String getMedia() {
				return proxied.getMedia();
			}

			public float getMediumFontSize() {
				return proxied.getMediumFontSize();
			}

			public float getPixelToMM() {
				return proxied.getPixelToMM();
			}

			public float getPixelUnitToMillimeter() {
				return proxied.getPixelUnitToMillimeter();
			}

			public ScriptSecurity getScriptSecurity(String arg0,
					ParsedURL arg1, ParsedURL arg2) {
				return proxied.getScriptSecurity(arg0, arg1, arg2);
			}

			public AffineTransform getTransform() {
				return proxied.getTransform();
			}

			public String getUserStyleSheetURI() {
				return proxied.getUserStyleSheetURI();
			}

			public Dimension2D getViewportSize() {
				return proxied.getViewportSize();
			}

			public String getXMLParserClassName() {
				return proxied.getXMLParserClassName();
			}

			public void handleElement(Element arg0, Object arg1) {
				proxied.handleElement(arg0, arg1);
			}

			public boolean hasFeature(String arg0) {
				return proxied.hasFeature(arg0);
			}

			public boolean isXMLParserValidating() {
				return proxied.isXMLParserValidating();
			}

			public void openLink(SVGAElement arg0) {
				log.info("opening "+arg0.getLocalName());
			}

			public void registerExtension(BridgeExtension arg0) {
				proxied.registerExtension(arg0);
			}

			public void setSVGCursor(Cursor arg0) {
				proxied.setSVGCursor(arg0);
			}

			public void setTextSelection(Mark arg0, Mark arg1) {
				proxied.setTextSelection(arg0, arg1);
			}

			public void setTransform(AffineTransform arg0) {
				proxied.setTransform(arg0);
			}

			public void showAlert(String msg) {
				log.info("alert: "+msg);
			}

			public boolean showConfirm(String arg0) {
				log.info("confirm: "+arg0);
				return false;
			}

			public String showPrompt(String arg0, String arg1) {
				log.info("show prompt: "+arg0+","+arg1);
				return arg0;
			}

			public String showPrompt(String arg0) {
				log.info("show prompt: "+arg0);
				return arg0;
			}

			public boolean supportExtension(String arg0) {
				return proxied.supportExtension(arg0);
			}
			
		};
	}
	
	
	
}
